package servlet.shopping;

import com.alibaba.fastjson.JSONObject;
import service.SearchIndexpagere;
import service.ShoppingService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/addshoping")
public class AddShopping extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //图书ID
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        //设置响应对象类型
        resp.setContentType("application/json;charset=utf-8");
        // 从请求中获取参数
//        SearchIndexpagere searchIndexpagere = new SearchIndexpagere();
        ShoppingService shoppingService = new ShoppingService();
        System.out.println("添加购物车操作:");
        int i = shoppingService.addShopping(id, username);
        System.out.println("添加购物车完成");
        // 创建 JSON 对象
        JSONObject jsonResponse = new JSONObject();
        if (i==1){
            // 设置响应的 JSON 数据
            jsonResponse.put("code", 1);
            // 将 JSON 对象转换为字符串
            String jsonString = jsonResponse.toJSONString();
            // 返回 JSON 字符串给客户端
            PrintWriter out = resp.getWriter();
            out.print(jsonString);
            out.flush();
        }else{
            jsonResponse.put("code", 0);
            // 将 JSON 对象转换为字符串
            String jsonString = jsonResponse.toJSONString();
            // 返回 JSON 字符串给客户端
            PrintWriter out = resp.getWriter();
            out.print(jsonString);
            out.flush();
        }


    }
}
